package org.lq.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.dao.PersonDao;
import org.lq.entity.Person;
import org.lq.util.JDBCUtil;

public class PersonDaoImpl implements PersonDao {
	
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;

	@Override
	public List<Person> findAll() {
		List<Person> list = new ArrayList<Person>();
		Person p = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement("select * from person");
			rs = ps.executeQuery();
			while(rs.next()) {
				p = new Person();
				p.setAddress(rs.getString("address"));
				p.setEducation(rs.getString("education"));
				p.setGender(rs.getString("gender"));
				p.setId(rs.getInt("id"));
				p.setName(rs.getString("name"));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		return list;
	}

	@Override
	public int add(Person p) {
		int num = 0;
		String sql = "insert into person(id,name,gender,education,address) values(null,?,?,?,?)";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, p.getName());
			ps.setString(2, p.getGender());
			ps.setString(3, p.getEducation());
			ps.setString(4, p.getAddress());
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		
		return num;
	}

	@Override
	public int update(Person p) {
		int num = 0;
		String sql = "update person set name=?,gender=?,education=?,address=? where id=?";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, p.getName());
			ps.setString(2, p.getGender());
			ps.setString(3, p.getEducation());
			ps.setString(4, p.getAddress());
			ps.setInt(5, p.getId());
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		return num;
	}

	@Override
	public int delete(int id) {
		int num = 0;
		String sql = "delete from person where id = ?";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1,id);
			num = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		
		return num;
	}

	@Override
	public List<Person> likeByName(String name) {
		List<Person> list = new ArrayList<Person>();
		Person p = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement("select * from person where name like ?");
			ps.setString(1, "%"+name+"%");
			rs = ps.executeQuery();
			while(rs.next()) {
				p = new Person();
				p.setAddress(rs.getString("address"));
				p.setEducation(rs.getString("education"));
				p.setGender(rs.getString("gender"));
				p.setId(rs.getInt("id"));
				p.setName(rs.getString("name"));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		return list;
	}

	@Override
	public List<Person> likeByPerson(Person person) {
		List<Person> list = new ArrayList<Person>();
		Person p = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement("select * FROM person WHERE name LIKE ? and gender LIKE ? and education LIKE ?");
			ps.setString(1, "%"+person.getName()+"%");
			ps.setString(2, "%"+person.getGender()+"%");
			ps.setString(3, "%"+person.getEducation()+"%");
			rs = ps.executeQuery();
			while(rs.next()) {
				p = new Person();
				p.setAddress(rs.getString("address"));
				p.setEducation(rs.getString("education"));
				p.setGender(rs.getString("gender"));
				p.setId(rs.getInt("id"));
				p.setName(rs.getString("name"));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(rs, ps, conn);
		}
		return list;
	}

}
